﻿/**

 * Copyright (c) 2015-2016, FastDev 刘强 (fastdev@163.com) & Quincy.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */


using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;

namespace OF.DB.DataAccess
{
    public class Util
    {
        private static ILog logger = null;
        static Util()
        {
            log4net.Config.XmlConfigurator.Configure();
            logger = LogManager.GetLogger(typeof(Util));
        }

        public static void LoopAction(Action action, int sleepIntervalSeconds)
        {
            try
            {
                while (true)
                {
                    try
                    {
                        action();
                    }
                    catch (Exception ex)
                    {
                        Util.DumpFile("DeleteLogAction Inner Exception:" + ex.ToString());
                    }
                    SleepNSeconds(sleepIntervalSeconds);
                }
            }
            catch (Exception ex)
            {
                SleepNSeconds(1);
                Util.DumpFile("DeleteLogAction Exit Exception:" + ex.ToString());
            }
        }

        public static void SleepNSeconds(int seconds)
        {
            for (int i1 = 0; i1 < seconds; i1++)
            {
                Thread.Sleep(1000);
            }
        }

        public static void EnsureExecute(Action action, int maxSleepSeconds)
        {
            while (true)
            {
                try
                {
                    action();
                    break;
                }
                catch (Exception ex)
                {
                    Util.DumpFile("Jpush thread save xml exception:" + ex.ToString());
                    SleepNSeconds(maxSleepSeconds);
                }
            }
        }

        public static void DumpFile(string str)
        {
            logger.Error(str);
        }

        public static void DumpFile(object obj, Exception ex)
        {
            string str = string.Empty;
            if (obj != null)
            {
                str = obj.ToString();
            }
            str += ", Exception is:" + System.Environment.NewLine + ex.ToString();
            logger.Error(str);
        }



        public static bool IsEquals(byte[] btArr1, byte[] btArr2)
        {
            if (btArr1 == btArr2)
            {
                return true;
            }
            else
            {
                if (btArr1 == null || btArr2 == null)
                {
                    return false;
                }
            }

            if (btArr1.Length != btArr2.Length)
            {
                return false;
            }

            for (int i1 = 0; i1 < btArr1.Length; i1++)
            {
                if (btArr1[i1] != btArr2[i1])
                {
                    return false;
                }
            }
            return true;
        }
    }


    public class IgnoreCaseComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            if (object.ReferenceEquals(x, y))
            {
                return true;
            }

            if (x == null || y == null)
            {
                return false;
            }
            return string.Equals(x, y, StringComparison.InvariantCultureIgnoreCase);
        }

        public int GetHashCode(string obj)
        {
            return obj == null ? 0 : obj.ToLower().GetHashCode();
        }
    }
}

